package BaseUrlsTest;
use strict;
use warnings;

use base qw(Test::Class);
use Test::More;
use Data::Dumper;

use lib '/home/'.$ENV{USER}.'/djpp/core/service';
use lib '/home/'.$ENV{USER}.'/djpp/core/model';
use lib '/home/'.$ENV{USER}.'/djpp/core/t';

use RealObjects;
use BaseUrls;
use UrlInfo;
use Time::HiRes qw(gettimeofday);

sub testSetup : Test(setup)
{
	my $self = shift;
	
	$self->{urls} = new RealObjects::urls;
}

sub testCreateUrls : Test
{
	my $self = shift;
	
	ok($self->{urls}, "Urls created");
}

sub testAddNewItem : Test
{
	my $self = shift;
	
	my $urls = $self->{urls};
	
	my $url = '^/rss/catalog$';
	
	$urls->add(
		url 	=> $url,
		view 	=> "RSS::Catalog::list",
		args 	=> {},
	);

	my $controller = $urls->handle('/rss/catalog');
	
	ok($controller->module eq "RSS::Catalog", "Controller OK");
	ok($controller->action eq "list", "Action OK");
}

sub testParseInputUrl : Test
{
	my $self = shift;
	
	my $urlInfo = UrlInfo->create('^/rss/catalog$');
	
	ok($urlInfo->beginString == 1, "Begin String OK");
	ok($urlInfo->endString == 1, "End String OK");
	ok($urlInfo->re eq qr!^/rss/catalog$!, "Url re OK");
}

sub testParseInputUrlWithArgs : Test
{
	my $self = shift;
	
	my $urlInfo = UrlInfo->create('^/rss/arch/(A<year>\d+)/(A<month>\d+)$');
	
	ok($urlInfo->beginString == 1, "Begin2 String OK");
	ok($urlInfo->endString == 1, "End2 String OK");
	ok($urlInfo->args->{year}, "YEAR OK");
	ok($urlInfo->args->{month}, "month OK");
}


sub testAddNewItemWithInnerArgs : Test
{ 
	my $self = shift;
	
	my $urls = $self->{urls};
	
	my $re = '^/rss/archive/(A<year>\d+)/(A<month>\d+)$';

	$urls->add(
		url 	=> $re,
		view 	=> "RSS::Catalog::archive",
		args 	=> {},
	);
	
	my $t0 = gettimeofday;
	my $controller = $urls->handle('/rss/arch/23/2009/08/');
	my $delta = gettimeofday - $t0;
	
	ok($controller->module eq "RSS::Catalog", "Controller OK: ".$controller->module);
	ok($controller->action eq "arch", "Action OK");
	ok($controller->args->{year} eq '2009', "Get YEAR parametr OK");
	ok($controller->args->{month} eq '08', "Get MONTH parametr OK");
	ok($delta < 0.0002, "Handle url more than 0.0002 sec: = ".$delta);
}

sub testAlternativeArgs : Test
{
	my $self = shift;
	
	my $urls = $self->{urls};
	
	my $controller = $urls->handle('/rss/arch/add/');
	
	ok($controller->args->{type} eq 'add', "Get TYPE parametr OK: ".$controller->args->{type});

	$controller = $urls->handle('/rss/arch/ass/');
	
	ok($controller->args->{type} eq 'ass', "Get TYPE parametr OK: ".$controller->args->{type});
	
}


1;
